/*
Copyright (C) 2012  Nikos Siatras
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
package ncore_tcp_port_forward.Core.AccessList;

import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author Nikos Siatras
 */
public class AccessList implements Serializable
{

    private ArrayList<String> fAccesslist;
    transient private ArrayList<String> fAccessListIPs;
    private boolean fIsEnabled = false;
    transient private Thread fUpdateAccessListIPsThread;

    public AccessList()
    {
        fAccesslist = new ArrayList<>();
        fAccessListIPs = new ArrayList<>();

        StartUpdatingIPs();

    }

    public void StartUpdatingIPs()
    {
        if (fUpdateAccessListIPsThread == null)
        {
            fAccessListIPs = new ArrayList<>();
            fUpdateAccessListIPsThread = new Thread(new Runnable()
            {

                @Override
                public void run()
                {
                    while (true)
                    {
                        if (isEnabled())
                        {
                            ArrayList<String> newIPs = new ArrayList<>();
                            for (String hostName : fAccesslist)
                            {
                                try
                                {
                                    // Get IP Address from hostname
                                    java.net.InetAddress ipAddress = java.net.InetAddress.getByName(hostName);
                                    newIPs.add(ipAddress.getHostAddress().toString());
                                    System.out.println(ipAddress.getHostAddress().toString());
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                            fAccessListIPs = newIPs;
                        }
                        try
                        {
                            Thread.sleep(20000);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            });
            fUpdateAccessListIPsThread.start();
        }
    }

    /**
     * Check if a host is in the AccessList
     *
     * @param host is the host to check
     * @return true or false
     */
    public boolean ValidateHost(String host)
    {
        if (fIsEnabled)
        {
            if (fAccessListIPs.contains(host))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        return true;
    }

    /**
     * Add a host to the access list
     *
     * @param host is the host to add
     */
    public synchronized void AddToAccessList(String host)
    {
        if (!fAccesslist.contains(host))
        {
            fAccesslist.add(host);
        }
    }

    /**
     * Remove host from access list
     *
     * @param host is the host to remove
     */
    public synchronized void RemoveFromAccessList(String host)
    {
        int indexToRemove = -1;
        for (int i = 0; i < fAccesslist.size(); i++)
        {
            if (fAccesslist.get(i).equals(host))
            {
                indexToRemove = i;
                break;
            }
        }
        if (indexToRemove > -1)
        {
            fAccesslist.remove(indexToRemove);
        }
    }

    public synchronized void ClearAccessList()
    {
        fAccesslist = new ArrayList<String>();
    }

    public ArrayList<String> getAccessList()
    {
        return fAccesslist;
    }
    
      public ArrayList<String> getAccessListIPs()
    {
        return fAccessListIPs;
    }

    public void setEnabled(boolean value)
    {
        fIsEnabled = value;
    }

    public boolean isEnabled()
    {
        return fIsEnabled;
    }
}
